package com.abvnet.project.water.project.controller;

import com.abvnet.common.utils.security.ShiroUtils;
import com.abvnet.framework.aspectj.lang.annotation.Log;
import com.abvnet.framework.aspectj.lang.enums.BusinessType;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.framework.web.page.TableDataInfo;
import com.abvnet.project.system.user.service.IUserService;
import com.abvnet.project.water.project.domain.ProjectInfo;
import com.abvnet.project.water.project.service.IProjectInfoService;
import com.abvnet.project.water.project.service.IProjectPartnerService;
import com.abvnet.project.water.project.service.IProjectProgressService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目信息操作处理
 * 
 * @author duj
 * @date 2019-07-02
 */
@Controller
@RequestMapping("/project/projectInfo")
@Slf4j
public class ProjectInfoController extends BaseController {
    private String prefix = "project/projectInfo";
	
	@Autowired
	private IProjectInfoService projectInfoService;

    @Autowired
    private IProjectProgressService projectProgressService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IProjectPartnerService projectPartnerService;

	@RequiresPermissions("project:projectInfo:list")
	@GetMapping()
	public String projectInfo()
	{
	    return prefix + "/projectInfo";
	}
	
	/**
	 * 查询项目列表
	 */
	@RequiresPermissions("project:projectInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ProjectInfo projectInfo) {
		if (!ShiroUtils.getSysUser().isAdmin()) { // 非管理员，只有数据可见中的用户能看到
            projectInfo.setCreateBy(ShiroUtils.getLoginName());
			projectInfo.setVisibleUserId(String.valueOf(ShiroUtils.getUserId()));
        }
		startPage();
        List<ProjectInfo> list = projectInfoService.selectProjectInfoList(projectInfo);
		return getDataTable(list);
	}

	/**
	 * 新增项目
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap) {
	    return prefix + "/add";
	}

	/**
	 * 新增保存项目
	 */
	@RequiresPermissions("project:projectInfo:add")
	@Log(title = "项目信息", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
    public AjaxResult addSave(ProjectInfo projectInfo) {
		return toAjax(projectInfoService.insertProjectInfo(projectInfo));
	}

	/**
	 * 修改项目
	 */
	@GetMapping("/edit/{projectId}")
	public String edit(@PathVariable("projectId") Long projectId, ModelMap mmap) {
		mmap.put("projectInfo", projectInfoService.selectProjectInfoById(projectId));
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存项目
	 */
	@RequiresPermissions("project:projectInfo:edit")
	@Log(title = "项目信息", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ProjectInfo projectInfo) {
		return toAjax(projectInfoService.updateProjectInfo(projectInfo));
	}

	/**
	 * 删除项目
	 */
	@RequiresPermissions("project:projectInfo:remove")
	@Log(title = "项目信息", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(projectInfoService.deleteProjectInfoByIds(ids));
	}

    @GetMapping("/progress/{projectId}")
    public String progress(@PathVariable("projectId") Long projectId, ModelMap mmap) {
        mmap.put("users",userService.selectProjectProgressUsers());
        mmap.put("list", projectProgressService.selectProjectProgressListByProjectId(projectId));
        return prefix + "/progress";
    }

    @GetMapping("/partner/{projectId}")
    public String partner(@PathVariable("projectId") Long projectId, ModelMap mmap) {
        mmap.put("users",projectPartnerService.selectUsersByProjectId(projectId));
        return prefix + "/partner";
    }

	/**
	 * 跳转到数据可见页面
	 */
	@GetMapping("/visible/{projectId}")
	public String visible(@PathVariable("projectId") Long projectId, ModelMap mmap) {
		mmap.put("users",projectPartnerService.selectUsersByProjectId(projectId));
		return prefix + "/visible";
	}

	/**
	 * 编辑数据可见
	 */
	@Log(title = "数据可见", businessType = BusinessType.UPDATE)
	@RequiresPermissions("project:projectInfo:edit")
	@PostMapping("/editVisible")
	@ResponseBody
	public AjaxResult editVisible(ProjectInfo projectInfo) {
		return toAjax(projectInfoService.updateProjectInfo(projectInfo));
	}

    @Log(title = "项目进度", businessType = BusinessType.UPDATE)
    @RequiresPermissions("project:projectInfo:edit")
    @PostMapping("/pay")
    @ResponseBody
    public AjaxResult pay(ProjectInfo projectInfo) {
        return toAjax(projectInfoService.updateProjectInfo(projectInfo));
    }

    @Log(title = "项目进度", businessType = BusinessType.UPDATE)
    @RequiresPermissions("project:projectInfo:edit")
    @PostMapping("/guide")
    @ResponseBody
    public AjaxResult guide(ProjectInfo projectInfo) {
        return toAjax(projectInfoService.updateProjectInfo(projectInfo));
    }
}